﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DTO;
using BUS;

namespace GUI
{
    public partial class frmThemHoaDonSanPham : Form
    {
        public frmThemHoaDonSanPham()
        {
            InitializeComponent();
        }

        public List<NguyenVatLieu_SanPhamDTO> dsnvl;
        public int manguyenvatlieu;

        private void frmThemHoaDonSanPham_Load(object sender, EventArgs e)
        {
            try
            {
                 dsnvl = new List<NguyenVatLieu_SanPhamDTO>();
                NguyenVatLieuBUS nvlbus = new NguyenVatLieuBUS();
                dsnvl = nvlbus.LayDanhSachNguyenVatLieu();
                cmb_dgv_TenNVL.DataSource = dsnvl;
                cmb_dgv_TenNVL.DisplayMember = "Ten";
                cmb_dgv_TenNVL.ValueMember = "MaNVL";
            }
            catch (Exception)
            {
                
                throw;
            }
            // load nguyen vat lieu len grid view

        }

        private void dgvChiTiet_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (dgvChiTiet.CurrentCell.ColumnIndex == 2)
            {
                ComboBox cbx = e.Control as ComboBox;


                if (cbx != null)
                {
                    //cmb_dgv_TenNVL.DataSource = dsnvl;
                    //cmb_dgv_TenNVL.DisplayMember = "Ten";
                    //cmb_dgv_TenNVL.ValueMember = "MaNVL";

                    cbx.SelectedIndexChanged -= comboBox_SelectedIndexChanged;
                    cbx.SelectedIndexChanged += comboBox_SelectedIndexChanged;
                }




            }
           
        }

        void comboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            //int selectedIndex = ((ComboBox)sender).SelectedIndex;
            //MessageBox.Show("Selected Index = " + selectedIndex);
            try
            {
                manguyenvatlieu = Convert.ToInt32(((ComboBox)sender).SelectedValue);
                foreach (NguyenVatLieu_SanPhamDTO nvl in dsnvl)
                {
                    if (nvl.MaNVL == manguyenvatlieu)
                    {
                        Double giaban = nvl.DonGiaBan;
                        string donvi = nvl.DonViTinh;
                        int manvl = nvl.MaNVL;
                        dgvChiTiet.Rows[dgvChiTiet.CurrentCell.RowIndex].Cells[1].Value = manvl.ToString();
                        dgvChiTiet.Rows[dgvChiTiet.CurrentCell.RowIndex].Cells[4].Value = donvi;
                        dgvChiTiet.Rows[dgvChiTiet.CurrentCell.RowIndex].Cells[5].Value = giaban;

                    }
                }
            }
            catch (Exception)
            {
                
                
            }
           
        }

        

        private void dgvChiTiet_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 3)
            {
                try
                {
                    if (dgvChiTiet.Rows[e.RowIndex].Cells[2].Value.ToString() != "" && dgvChiTiet.Rows[e.RowIndex].Cells[5].Value.ToString() != "")
	                {
		                 int soluong = Convert.ToInt32(dgvChiTiet.Rows[e.RowIndex].Cells[3].Value.ToString());
                        double dongia = Convert.ToDouble(dgvChiTiet.Rows[e.RowIndex].Cells[5].Value.ToString());
                        double thanhtien = soluong * dongia;
                        dgvChiTiet.Rows[e.RowIndex].Cells[6].Value = thanhtien.ToString();
	                }
                   
                }

                catch(Exception)
                {
                    
                }
            }

            // add stt
            int stt = 1;
            for (int i = 0; i < dgvChiTiet.Rows.Count; i++)
            {
                dgvChiTiet.Rows[i].Cells[0].Value = stt.ToString();
                stt++;
            }

            // tinh tong tien
          
                double tongtien = 0;
                for (int i = 0; i < dgvChiTiet.Rows.Count; i++)
                {
                    try
                    {
                        if (dgvChiTiet.Rows[i].Cells[6].Value != "")
                        {
                            double thanhtien = Convert.ToDouble(dgvChiTiet.Rows[i].Cells[6].Value);
                            tongtien += thanhtien;
                        }
                    }
                    catch (Exception)
                    {


                    }
                }

                lbTongTien.Text = tongtien.ToString();
            
           
           
        }

        private void dgvChiTiet_CellClick(object sender, DataGridViewCellEventArgs e)
        {
           
        }

        private void buttonX3_Click(object sender, EventArgs e)
        {
            int id = dgvChiTiet.CurrentCell.RowIndex;
            dgvChiTiet.Rows.RemoveAt(id);
            
        }

        private void buttonX4_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            // them hoa don
            HoaDonSanPhamBUS hdbus = new HoaDonSanPhamBUS();
            HoaDonSanPhamDTO hddto = new HoaDonSanPhamDTO();
            hddto.NgayLap = Convert.ToDateTime(dtpNgayLap.Text);
            hddto.TongTien = Convert.ToDouble(lbTongTien.Text.ToString());
            try
            {

            }
            catch (Exception)
            {

                hddto.MaKH = Convert.ToInt32(txt_thekh.Text.ToString());
            }
            

            int ID = hdbus.ThemHoaDonSanPham(hddto);

            // them chi tiet
            CT_HoaDonSanPhamBUS cthdbus = new CT_HoaDonSanPhamBUS();

            for (int i = 0; i < dgvChiTiet.Rows.Count; i++)
            {
                try
                {
                    CT_HoaDonSanPhamDTO cthdsp = new CT_HoaDonSanPhamDTO();
                    cthdsp.MaHDSP = ID;
                    cthdsp.MaNVL = Convert.ToInt32(dgvChiTiet.Rows[i].Cells[1].Value);
                    cthdsp.SoLuong = Convert.ToInt32(dgvChiTiet.Rows[i].Cells[3].Value);
                    if (cthdsp.MaNVL != 0)
                    {
                        cthdbus.ThemChiTietHoaDonSanPham(cthdsp);
                    }
                   
                    
                }
                catch (Exception)
                {
                    
                    
                }
               

            }

            if (txt_thekh.Text != "" )
            {
                int diemtichluy = 0;
                // cap nhat diem tich luy
                if (Convert.ToDouble(lbTongTien.Text) >= 500000 && Convert.ToDouble(lbTongTien.Text) <= 1000000)
                {
                    diemtichluy = 10;
                }

                if (Convert.ToDouble(lbTongTien.Text) >= 1000000 && Convert.ToDouble(lbTongTien.Text) <= 2000000)
                {
                    diemtichluy = 25;
                }

                if (Convert.ToDouble(lbTongTien.Text) >= 2000000 && Convert.ToDouble(lbTongTien.Text) <= 3000000)
                {
                    diemtichluy = 40;
                }

                if (Convert.ToDouble(lbTongTien.Text) >= 3000000 && Convert.ToDouble(lbTongTien.Text) <= 4000000)
                {
                    diemtichluy = 55;
                }

                if (Convert.ToDouble(lbTongTien.Text) >= 4000000 && Convert.ToDouble(lbTongTien.Text) <= 5000000)
                {
                    diemtichluy = 70;
                }

                if (Convert.ToDouble(lbTongTien.Text) >= 5000000 && Convert.ToDouble(lbTongTien.Text) <= 6000000)
                {
                    diemtichluy = 90;
                }

                if (Convert.ToDouble(lbTongTien.Text) >= 6000000 )
                {
                    diemtichluy = 120;
                }

                KhachHangBUS khbus = new KhachHangBUS();
                int flag = khbus.CapNhatDiemTichLuy(Convert.ToInt32(txt_thekh.Text),diemtichluy);

            }
            
            MessageBox.Show("Them thanh cong!");

            

        }

      
    }
}
